<template>
    <div class="page">
        <breadcrumb :breadcrumb="breadcrumb"></breadcrumb>
        <div class="container">
            <div class="row">
                <div class="header-toolbar clearfix">
                    <toolbar :options="options" :defaultSort="defaultSort"></toolbar>
                </div>
                <each-list :productList="productList"></each-list>
                <div class="bottom-toolbar clearfix">
                    <toolbar :showSort="false"></toolbar>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import api from '../http'
import mixin from '../mixin'
import breadcrumb from '../components/common/Breadcrumbs'
import toolbar from '../components/Product/ToolBar'
import EachList from '../components/Product/EachList'
export default {
    mixins: [mixin],
    data() {
        return {
            defaultSort: '',
            options: [],
            breadcrumb: [],
            productList: []
        }
    },
    mounted() {
        this.init()
    },
    methods: {
        init() {
            api.GetProductList().then(response => {
                this.defaultSort = this.$route.query.sort ? this.$route.query.sort : response.data.defaultSort
                this.options = response.data.options
                this.breadcrumb = response.data.breadcrumb
                this.productList = response.data.items
            })
        }
    },
    components: {
        breadcrumb,
        toolbar,
        EachList
    },
    watch: {
        defaultSort(val, oldval) {
            var sortName //排序字段
            var isAsc //asc or desc ===> false or true
            var o_type // 字段类型  
            switch (val) {
                case 'oakleycaRelevanceSort':
                    sortName = 'color_length'
                    isAsc = true
                    o_type = parseInt
                    break;
                case 'oakleycaName-ascSort':
                    sortName = 'product_name'
                    isAsc = false
                    o_type = String
                    break;
                case 'oakleycaName-descSort':
                    sortName = 'product_name'
                    isAsc = true
                    o_type = String
                    break;
                case 'oakleycaPriceValue-ascSort':
                    sortName = 'special_price'
                    isAsc = false
                    o_type = parseFloat
                    break;
                case 'oakleycaPriceValue-descSort':
                    sortName = 'special_price'
                    isAsc = true
                    o_type = parseFloat
                    break;
                default:
                    sortName = 'product_name'
                    isAsc = false
                    o_type = String
                    break;
            }
            if (val) {
                return this.productList.sort(this.sortBy(sortName, isAsc, o_type))
            } else {
                return this.productList
            }
        }
    }
}
</script>
